﻿using _9jCardRegisterCollection.Models;
using AntdUI;
using CardRegisterCollection.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9jCardRegisterCollection.Views
{
    public partial class DomesticAttendeePage : UserControl
    {
        public DomesticAttendeePage(AntdUI.Window _window)
        {
            window = _window;
            InitializeComponent();

            //初始化表格列头
            InitTableColumns();
            InitData();

            //BindEventHandler();
        }
        AntdUI.Window window;
        AntList<DomesticAttendeeForTable> domesticAttendees;
        DomesticAttendee domesticAttendeeBll = new DomesticAttendee();
        private void InitTableColumns()
        {
            tabDomesticAttendee.Columns = new ColumnCollection() {
                new ColumnCheck("Selected"){Fixed = true},
                new Column("Id", "人员ID", ColumnAlign.Center)
                {
                    Visible = false,
                },
                new Column("Name", "人员名称", ColumnAlign.Center)
                {
                    Width="250",
                    //设置树节点，名称需和User里的User[]名称保持一致
                    KeyTree = "ChildTypes",
                    Align = ColumnAlign.Left,
                    ColAlign = ColumnAlign.Center
                },
                new Column("Gender", "性别", ColumnAlign.Center) ,
                new Column("IDNumber", "身份证号码", ColumnAlign.Center) ,
                new Column("WorkUnit", "工作单位",ColumnAlign.Center),
                new Column("CardType", "办证类型",ColumnAlign.Center),
                new Column("UserDefine1", "组别",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center)
                {
                    Fixed = true,//冻结列
                }
            };
        }

        private void InitData()
        {
            domesticAttendees = new AntList<DomesticAttendeeForTable>();



            DataSet ds = new DataSet();
            if (txtKeyword.Text.Trim().Length > 0)
            {
                string strWhere = " name like '%" + txtKeyword.Text.ToString() + "%'";
                ds = domesticAttendeeBll.Query(strWhere);
            }
            else
            {
                ds = domesticAttendeeBll.GetList();
            }
            //有数据
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    domesticAttendees.Add(new DomesticAttendeeForTable
                    {
                        Id = dr["Id"].ToString(),
                        //BatchNo = dr["BatchNo"].ToString(),
                        Name = dr["name"].ToString(),
                        //BirthDate = dr["BirthDate"].ToString(),
                        Gender = dr["Gender"].ToString(),
                        //RegistrationProvince = dr["RegistrationProvince"].ToString(),
                        //RegistrationCity = dr["RegistrationCity"].ToString(),
                        //RegistrationAddress = dr["RegistrationAddress"].ToString(),
                        //CurrentResidenceProvince = dr["CurrentResidenceProvince"].ToString(),
                        //CurrentResidenceCity = dr["CurrentResidenceCity"].ToString(),
                        //CurrentResidenceAddress = dr["CurrentResidenceAddress"].ToString(),
                        //IDType = dr["IDType"].ToString(),
                        IDNumber = dr["IDNumber"].ToString(),
                        CardType = dr["CardType"].ToString(),
                        //HolderOfCertificateType = dr["HolderOfCertificateType"].ToString(),
                        //TrafficArea = dr["TrafficArea"].ToString(),
                        WorkUnit = dr["WorkUnit"].ToString(),

                        //Telephone = dr["Telephone"].ToString(),
                        //Remark = dr["Remark"].ToString(),
                        UserDefine1 = dr["UserDefine1"].ToString(),
                        //UserDefine2 = dr["UserDefine2"].ToString(),
                        //UserDefine3 = dr["UserDefine3"].ToString(),
                        //UserDefine4 = dr["UserDefine4"].ToString(),
                        //UserDefine5 = dr["UserDefine5"].ToString(),
                        //UserDefine6 = dr["UserDefine6"].ToString(),
                        //UserDefine7 = dr["UserDefine7"].ToString(),
                        //UserDefine8 = dr["UserDefine8"].ToString(),
                        //UserDefine9 = dr["UserDefine9"].ToString(),
                        //UserDefine10 = dr["UserDefine10"].ToString(),
                        ////DeleteFlag = (bool)dr["DeleteFlag"],
                        //CreatedBy = dr["CreatedBy"].ToString(),
                        //CreatedAt = Convert.ToDateTime(dr["CreatedAt"].ToString()),
                        //LastUpdatedBy = dr["LastUpdatedBy"].ToString(),
                        //LastUpdatedAt = Convert.ToDateTime(dr["LastUpdatedAt"].ToString()),
                        CellLinks = new CellLink[] {
                            new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
                            new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error) },
                    });
                }
            }
            else
            {
                domesticAttendees.Clear();
            }
            tabDomesticAttendee.Binding(domesticAttendees);
        }

        private void butSearch_Click(object sender, EventArgs e)
        {
            InitData();
        }

        private void tabDomesticAttendee_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;
            if (e.Record is DomesticAttendeeForTable domesticAttendeeForTable)
            {
                CardRegisterCollection.Model.DomesticAttendee domesticAttendee = new CardRegisterCollection.Model.DomesticAttendee()
                {
                    Id = domesticAttendeeForTable.Id,
                    Name = domesticAttendeeForTable.Name,
                    Gender = domesticAttendeeForTable.Gender,
                    IDNumber = domesticAttendeeForTable.IDNumber,
                    WorkUnit = domesticAttendeeForTable.WorkUnit,
                    CardType = domesticAttendeeForTable.CardType,
                    UserDefine1 = domesticAttendeeForTable.UserDefine1
                };


                switch (buttontext)
                {
                    //暂不支持进入整行编辑，只支持指定单元格编辑，推荐使用弹窗或抽屉编辑整行数据
                    case "编辑":
                        {
                            var form = new DomesticAttendeeEdit(window, domesticAttendee) { Size = new Size(500, 300) };
                            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
                            {
                                OnLoad = () =>
                                {
                                    AntdUI.Message.info(window, "进入编辑", autoClose: 1);
                                },
                                OnClose = () =>
                                {
                                    AntdUI.Message.info(window, "结束编辑", autoClose: 1);
                                    if (form.SureToChange)
                                    {
                                        if (domesticAttendeeBll.Update(domesticAttendee))
                                        {
                                            AntdUI.Message.success(window, "编辑成功", autoClose: 1);
                                            InitData();
                                        }
                                    }
                                }
                            });
                        }
                        break;
                    case "删除":
                        {
                            var result = Modal.open(window, "删除警告！", "确认要删除选择的数据吗？", TType.Warn);
                            if (result == DialogResult.OK)
                            {
                                domesticAttendeeBll.Delete(domesticAttendee.Id);
                                InitData();
                            }
                            break;

                        }
                    default:
                        break;
                }
            }
        }

        private void buttonADD_Click(object sender, EventArgs e)
        {
            CardRegisterCollection.Model.DomesticAttendee domesticAttendee = new CardRegisterCollection.Model.DomesticAttendee();
            var form = new DomesticAttendeeEdit(window, domesticAttendee) { Size = new Size(500, 300) };
            AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
            {
                OnLoad = () =>
                {
                    AntdUI.Message.info(window, "开始添加", autoClose: 1);

                },
                OnClose = () =>
                {
                    AntdUI.Message.info(window, "结束添加", autoClose: 1);
                    if (form.SureToChange)
                    {
                        if (domesticAttendeeBll.Add(domesticAttendee))
                        {
                            AntdUI.Message.success(window, "添加成功", autoClose: 1);
                            InitData();
                        }
                    }
                }
            });

        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            DomesticImportExcel form = new DomesticImportExcel(window);
            form.ShowDialog();
            InitData();
        }

        private void btnExport_Click(object sender, EventArgs e)
        { 
            DomesticExportExcel form = new DomesticExportExcel(window, domesticAttendees);
            form.ShowDialog();
            InitData();
        }
    }
}
